from rest_framework import serializers

from apps.article.models import Channel, Article, Comment
from apps.user.models import User


class ChannelsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Channel
        fields = ['id', 'name']


class ArticleSerializerSimple(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ("id", "title")


class UserSerializer(serializers.ModelSerializer):
    articles = ArticleSerializerSimple(read_only=True, many=True)

    class Meta:
        model = User
        fields = '__all__'


class UploadArticlesSerializer(serializers.ModelSerializer):
    image = serializers.CharField(required=False, default='', allow_blank=True)

    class Meta:
        model = Article
        exclude = ('collected_users',)


class ListArticleSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)
    collected = serializers.BooleanField(default=False)

    class Meta:
        model = Article
        fields = ("id", "title", "content", "createtime", "user", "collected_users", "collected", "image", "visits")


class CommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        fields = '__all__'


class DetailCommentSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)
    subs = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = Comment
        fields = ('id', 'content', 'article', 'user', 'parent', 'subs', 'createtime')


class DetailCommentSerializerList(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)
    subs = DetailCommentSerializer(read_only=True, many=True)

    class Meta:
        model = Comment
        fields = '__all__'


class DetailArticleSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)
    comments = DetailCommentSerializerList(read_only=True, many=True)

    class Meta:
        model = Article
        fields = "__all__"


class ArticleSearchSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = '__all__'
